<body>
<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 表格工具栏 -->
            <form class="layui-form toolbar">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <select name="field_name" lay-verify="required" required>
                            <option value="uid">用户ID</option>
                            <option value="account">账号</option>
                        </select>
                    </div>
                    <div class="layui-inline">
                        <input name="field_value" class="layui-input" placeholder="输入搜索内容"/>
                    </div>
                    <div class="layui-inline">
                        <div class="layui-input-inline">
                            <select name="status">
                                <option value="">审核状态</option>
                                <option value="0">待审核</option>
                                <option value="1">通过</option>
                                <option value="2">驳回</option>
                            </select>
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <select name="date_type" lay-verify="required" required>
                            <option value="created_at">创建时间</option>
                            <option value="updated_at">审核时间</option>
                        </select>
                    </div>
                    <div class="layui-inline">
                        <input type="text" autocomplete="off" id="startDate" name="start_date" class="layui-input"
                               placeholder="开始日期">
                    </div>
                    <div class="layui-inline">
                        <input type="text" autocomplete="off" id="endDate" name="end_date" class="layui-input"
                               placeholder="结束日期">
                    </div>
                    <div class="layui-inline">&emsp;
                        <button class="layui-btn icon-btn" lay-filter="search" lay-submit>
                            <i class="layui-icon">&#xe615;</i>搜索
                        </button>
                    </div>
                </div>
            </form>
            <!-- 数据表格 -->
            <table id="test" lay-filter="test"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="barDemo">
    {{# if(d.status == 0) { }}
    <a class="layui-btn layui-btn-success layui-btn-xs" lay-event="pass">通过</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="reject">驳回</a>
    {{# } }}
    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="del">删除</a>
</script>

<!-- js部分 -->
<script type="text/javascript" src="__TPL_RESOURCE_BACKEND__/libs/layui/layui.js"></script>
<script type="text/javascript" src="__TPL_RESOURCE_BACKEND__/js/common.js"></script>
<script>
    layui.use(['layer', 'form', 'table', 'admin','laydate'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var laydate = layui.laydate;

        const startTime = new Date(new Date().setDate(new Date().getDate() - 30));
        startTime.setHours(0, 0, 0, 0);
        var startDate = startTime.toISOString().split('T')[0] + ' 00:00:00';

        const endTime = new Date(new Date().setDate(new Date().getDate() + 2));
        endTime.setHours(0, 0, 0, 0);
        var endDate = endTime.toISOString().split('T')[0] + ' 00:00:00';

        laydate.render({
            elem: '#startDate',
            type: 'datetime',
            value: startDate
        });

        laydate.render({
            elem: '#endDate',
            type: 'datetime',
            value: endDate
        });

        /* 渲染表格 */
        var insTb = table.render({
            elem: '#test',
            url: "{{:url('Recharge/index')}}",
            page: {
                limit:20
            },
            where: {
                start_date: startDate,
                end_date: endDate
            },
            method: 'post',
            contentType: 'application/json',
            toolbar: true,
            cols: [[
                {field: 'id',title: 'ID',width: 50},
                {field: 'user_account_txt', title: '账户',width: 150},
                {field: 'money', title: '充值金额',width: 150},
                {field: 'balance', title: '余额',width: 150},
                {
                    title: '状态', templet: function (d) {
                        return [
                            '<span class="layui-badge layui-badge-gray">待审核</span>',
                            '<span class="layui-badge layui-badge-green">通过</span>',
                            '<span class="layui-badge layui-badge-red">驳回</span>'
                        ][d.status];
                    }, align: 'center', width: 80
                },
                {field: 'remark', title: '备注'},
                {field: 'created_at', title: '创建时间',width: 160},
                {field: 'updated_at', title: '审核时间',width: 160},
                {title: '操作', toolbar: '#barDemo', align: 'center', width: 160}
            ]]
        });

        /* 表格搜索 */
        form.on('submit(search)', function (data) {
            insTb.reload({where: data.field, page: {curr: 1}});
            return false;
        });

        /* 表格工具条点击事件 */
        table.on('tool(test)', function (obj) {
            if (obj.event === 'pass') {
                check(obj.data,1);
            } else if(obj.event === 'reject') {
                check(obj.data,2);
            } else if (obj.event === 'del'){ //删除
                doDel(obj);
            }
        });

        /* 删除 */
        function doDel(obj) {
            layer.confirm('确定要删除选中数据吗？', {
                skin: 'layui-layer-admin',
                shade: .1
            }, function (i) {
                layer.close(i);
                var loading = layer.load(3);
                $.ajax({
                    url: "{{:url('Recharge/del')}}",
                    type: 'post',
                    data: {id: obj.data.id},
                    success: function (res) {
                        if (res.code != 0) {
                            layer.msg(res.msg, {icon: 5,time:1000});
                        } else {
                            layer.msg(res.msg, {icon: 1,time:1500});
                            insTb.reload();
                        }
                        layer.close(loading);
                    }, error: function () {
                        layer.msg('网络错误请稍后重试', {icon: 5,time:1000});
                        layer.close(loading);
                    }
                });
                return false;
            });
        }

        /* 审核 */
        function check(obj,status) {
            layer.confirm('确定要审核选中数据吗？', {
                skin: 'layui-layer-admin',
                shade: .1
            }, function (i) {
                layer.close(i);
                var loading = layer.load(3);
                $.ajax({
                    url: "{{:url('Recharge/check')}}",
                    type: 'post',
                    data: {id: obj.id,status:status},
                    success: function (res) {
                        if (res.code != 0) {
                            layer.msg(res.msg, {icon: 5,time:1000});
                        } else {
                            layer.msg(res.msg, {icon: 1,time:1500});
                            insTb.reload();
                        }
                        layer.close(loading);
                    }, error: function () {
                        layer.msg('网络错误请稍后重试', {icon: 5,time:1000});
                        layer.close(loading);
                    }
                });
                return false;
            });
        }
    });
</script>
</body>
</html>
